#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[505][505];
bool vis[505];

signed main() {
	int n, p;
	cin >> n;

	int ans1 = 0, ans2 = 0;
	vector<int> v1, v2;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
		if (a[i][1] == i) {
			ans1++;
			v1.push_back(i);

			if (!vis[a[i][1]]) {
				ans2++;
				vis[a[i][1]] = 1;
				v2.push_back(a[i][1]);
			}
		}
		if (!vis[a[i][n]]) {
			ans2++;
			vis[a[i][n]] = 1;
			v2.push_back(a[i][n]);
		}
	}
	cin >> p;
	if (p == 1) {
		cout << ans1 << endl;
		for (int i : v1)
			cout << i << " ";
	} else {
		sort(v2.begin(), v2.end());
		cout << ans2 << endl;
		for (int i : v2)
			cout << i << " ";
	}
	return 0;
}
